import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:video_player/video_player.dart';

class VideoPlayerScreen2 extends StatefulWidget {
  @override
  _VideoPlayerScreenState createState() => _VideoPlayerScreenState();
}

class _VideoPlayerScreenState extends State<VideoPlayerScreen2> {
  final _platform =const MethodChannel('samples.flutter.dev/battery');
  late VideoPlayerController _controller;

  Future<void> _routerNative() async{
    try{
      await _platform.invokeMethod<int>('routerNative');
    } on PlatformException catch(e){
      print("channel 失效");
    }

  }

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network('https://consumer.huawei.com/content/dam/huawei-cbg-site/common/mkt/v4/about-us/video/video_play1.mp4')
      ..initialize().then((_) {
        setState(() {});
      });
    _controller.initialize().whenComplete(() => _controller.play());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('Video Player')),
        body: Center(
          child: _controller.value.isInitialized
              ? AspectRatio(
            aspectRatio: _controller.value.aspectRatio,
            child: VideoPlayer(_controller),
          )
              : CircularProgressIndicator(),
        ),
        floatingActionButton: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            ElevatedButton(
              child: Text('跳转原生'),
              onPressed: () => _routerNative()
            ),
            FloatingActionButton(
              onPressed: () {
                setState(() {
                  _controller.value.isPlaying
                      ? _controller.pause()
                      : _controller.play();
                });
              },
              child: Icon(
                _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
              ),
            ),
          ],
        )

    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
}